package org.eoti.math.cf;

import org.eoti.math.Fraction;

import java.io.PrintStream;
import java.math.BigDecimal;

/*
 * The MIT License
 *
 * Copyright (c) 2009 Malachi de AElfweald <malachid@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
public class CFDebug
{
	public static void main(String[] args)
	{
		if(args.length != 1)
		{
			System.out.format("USAGE:\tjava org.eoti.math.cf.CFUtil ContinuedFractionName\n");
			System.out.format("Example:\tjava org.eoti.math.cf.CFUtil org.eoti.math.cf.constants.pi\n");
			//System.out.format("Example:\tjava org.eoti.math.cf.CFUtil org.eoti.math.cf.constants.phi\n");
			System.out.format("Example:\tjava org.eoti.math.cf.CFUtil org.eoti.math.cf.constants.A001622\n");
			//System.out.format("Example:\tjava org.eoti.math.cf.CFUtil org.eoti.math.cf.constants.Phi\n");
			System.out.format("Example:\tjava org.eoti.math.cf.CFUtil org.eoti.math.cf.constants.A094214\n");
			System.out.format("Example:\tjava org.eoti.math.cf.CFUtil org.eoti.math.cf.constants.e\n");
			System.out.format("Example:\tjava org.eoti.math.cf.CFUtil org.eoti.math.cf.constants.tan\n");
			System.out.format("Example:\tjava org.eoti.math.cf.CFUtil org.eoti.math.cf.constants.A052119\n");
			System.out.format("Example:\tjava org.eoti.math.cf.CFUtil org.eoti.math.cf.constants.A073333\n");
			System.out.format("Example:\tjava org.eoti.math.cf.CFUtil org.eoti.math.cf.constants.A113011\n");
			System.exit(0);
		}

		try{
			Class clz = Class.forName(args[0]);
			debug( ( ContinuedFraction ) clz.newInstance() );
		}catch(Exception e){
			System.err.format("ERROR: %s\n", e.getMessage());
			e.printStackTrace();
		}
	}

	public static void debug(ContinuedFraction cf)
	{
		System.out.format("ContinuedFraction: %s\n\n", cf);
		System.out.format("Representation: %s\n\n", CFUtil.normalize(cf, 10));

		/**
		 * This doesn't quite work yet. Maybe change it to use the website or something?
		 */
//			File png = CFLaTex.createPNG(cf, 10);
//			if(png != null)
//				System.out.format("PNG Created: %s\n", png.getAbsolutePath());

		System.out.format("LaTex: %s\n\n", CFLaTex.createLaTex( cf, 10 ));
		System.out.format("First 100 digits:\n%s\n\n", CFUtil.toString(cf, 100));
		System.out.format("Continuous:\n");
		debugDump(cf, System.out);
		System.out.format("\n\n");
	}

	public static void debugDump(ContinuedFraction cf, PrintStream out)
	{
		// @TODO printed will eventually get really big. change this to hack off the beginning and use an index
		String printed = "";
		Fraction last = null;
		int scale = 50;

		for(Fraction fraction : cf.getConvergents())
		{
			if(fraction == null) break;
			if(last == null)
			{
				last = fraction;
				continue;
			}
			BigDecimal common = fraction.inCommon( last,  scale);
			if(common != null)
			{
				String s = common.toPlainString();
				if(s.length() > (scale/2))
					scale = scale * 2;
				
				out.format("%s", s.substring(printed.length()));
				printed = s;
				out.flush();
			}
			last = fraction;
		}

		if(last != null)
		{
			String tmp = last.compute(scale).stripTrailingZeros().toPlainString();
			if(printed.length() == 0)
			{
				out.format("%s", tmp);
			}else{
				if(tmp.startsWith(printed))
					out.format("%s", tmp.substring(printed.length()));
			}
		}

		out.println();
		out.flush();
	}
}
